package com.ytc.content.controller;

import com.ytc.commons.model.vo.CoursePubVo;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.utils.BeanHelper;
import com.ytc.content.domain.CoursePub;
import org.springframework.web.bind.annotation.*;
import com.ytc.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>

 */
@Slf4j
@RestController
@RequestMapping("/coursePub")
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;

    /*
    * //课程预览
    * 请求地址GET : /preview/{courseBaseId}
    * 参数:
    * 返回：ModelAndView
    *    数据：Map
    * */
    @GetMapping("/preview/{courseBaseId}")
    public ModelAndView preview(@PathVariable("courseBaseId")Long courseBaseId){
        ModelAndView mv = new ModelAndView();
        //指定模板
        mv.setViewName("learing_article");
        //调用service
        Map<String,Object> map = coursePubService.preview(courseBaseId);
        //指定数据
        mv.addAllObjects(map);
        return mv;
    }
    /*
    * //课程发布
    * POST /publish/{courseBaseId}
    * */
    @PostMapping("/publish/{courseBaseId}")
        public ResponseResult publishCourse(@PathVariable("courseBaseId") Long courseBaseId)throws Exception{
            return coursePubService.publishCourse(courseBaseId);
        }


    /*
    * 查询已发布课程
    * 地址： Get ？findAll
    *返回值： ResponseResult<List<CoursePubVo>>
    * */
    @GetMapping("/findAll")
    public ResponseResult<List<CoursePubVo>>findAll(){
        //查询所有
        List<CoursePub> list = coursePubService.list();
        List<CoursePub> coursePubs = BeanHelper.copyWithCollection(list, CoursePub.class);
        return ResponseResult.okResult(coursePubs);
    }

    /*
    根据id查询已发布课程
    */
    @GetMapping("/{id}")
    public ResponseResult<CoursePubVo> findById(@PathVariable("id") Long id){
        CoursePub coursePub = coursePubService.getById(id);
        CoursePubVo coursePubVo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
        return ResponseResult.okResult(coursePubVo);

    }



}
